#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/wait.h>
void writer(wfd)
{
	const char *str = "hello str, i am child!";
	char buffer[128];
	int cnt = 0;
	
	pid_t pid = getpid();
	while(1)
	{
        sleep(1);
        char c = 'A';
        write(wfd,&c,1);
		cnt++;
        printf("cnt:%d,\n",cnt);
	}
    close(wfd);
}
void reader(rfd)
{
	char buffer[1024];
    int cnt =10;
	while(1)
	{
		ssize_t n = read(rfd,buffer,sizeof(buffer)-1);
        if(n > 0)
            printf("father get a message:%s,n:%ld",buffer,n);
        else if(n == 0)
        {
			printf("read done!\n");
            break;
        }
        else
        {
        	break;
        }
        cnt--;
        if(cnt ==0)break;
	}
    close(rfd);
    printf("read end point close!!\n");
}
int main()
{
	int pipfd[2]; 
	int n =  pipe(pipfd);
	if(n<0)
	return 1;
	printf("%d %d",pipfd[0],pipfd[1]);		 // 3,4 ,0下标是读取,1下标是写入
	pid_t id = fork();
	if(id==0) 
	{
		// 写入数据,子进写
		close(pipfd[0]);
		writer(pipfd[1]);
		exit(0);
	}
	close(pipfd[1]);
	reader(pipfd[0]);
    
	int status =0;
    pid_t rid = waitpid(id,&status,0);
    if(rid == id)
    {
        printf("exit code:%d,exit signal:%d \n",WEXITSTATUS(status),status&0x7F);
    }
	return 0;
}